home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 6⁄22⁄90 / 0144-Re Global memory and-Jun90 < prev    next >
Encoding:
Text File  |  1990-06-25  |  3.7 KB  |  75 lines  |  [TEXT/GEOL]

  1. Item    2673838                         20-June-90        18:16PDT
  2.  
  3. From:   ROSENSTEIN1                     Rosenstein, Larry
  4.  
  5. To:     MM.XOBJ                         MacroMind, Haim Zamir,PRT
  6.  
  7. cc:     CPLUS.APPLE$                    C++ Interest List--Apple Employees
  8.         CPLUS.DEV$                      C++ Interest List--Developers
  9.  
  10. Sub:    Re: Global memory and…
  11.  
  12. I have to start with the disclaimer that I'm not an expert on the C++ runtime.
  13. I think I know how it works, and I tried out a few simple programs.  Hopefully,
  14. someone more knowledgable will point out any mistakes.
  15.  
  16. First, if you stick to using descendants of PascalObject then the runtime is
  17. the same as Object Pascal.  Object Pascal was designed to optimize space at the
  18. expense of method dispatching time.  You also can use the dispatching
  19. optimizations built into the linker.  It turns out that you can use many of
  20. C++'s features with Object Pascal (constructors, non-virtual functions, pure
  21. virtual functions, to name 3).
  22.  
  23. If you use C++ native classes, then the question is whether the class has any
  24. virtual members or not.  If the class is not intended to be subclassed (which
  25. is occasionally valid), then a class is the same as a struct and member
  26. function calls are the same as any function call.
  27.  
  28. Once you have a virtual function in the class, then things start to get
  29. interesting.  Each class with one or more virtual functions has one or more
  30. vtables.  The vtables contain information about the virtual functions of the
  31. class.  Each instance of a class has pointers to its vtables.  (You can have >1
  32. vtable in a class when multiple inheritance is involved.)
  33.  
  34. The vtables are in global space, and are initialized as part of the standard C
  35. global initialization.  However, the only direct reference to a vtable is from
  36. a global variable, which is also an initialized global.
  37.  
  38. Consequently, I don't think it is necessary that the vtables be located within
  39. 32K of A5, although the pointer must be.  A big C++ program may exceed the 32K
  40. limit on the global data segment, but if you use the -ss option to increase the
  41. segment size and the -srt option to sort the global data (putting the vtables
  42. farther from the start of the segment) then you won't have a problem.
  43.  
  44. If you have a base class B and make a derived class D, then the vtable for D
  45. will be a copy of the vtable for B, with D-specific methods added to the end.
  46. You can see that if you have many classes, the space for vtables can add up.  I
  47. think each entry in the vtable is 8 bytes long.  (This has do to C++'s emphasis
  48. on speed over space.  Copying the method table means that dispatching is a
  49. constant-time operation.)
  50.  
  51. The vtable contains a pointer to the actual method, and on the Macintosh, this
  52. must be a pointer to a jump table entry.  There is a limit of 4096 (or so) jump
  53. table entries, right now.  (In Object Pascal, methods that aren't overridden
  54. are not referenced from any method table, and don't necessarily need a jump
  55. table entry.)
  56.  
  57. Finally, a C++ method call requires about 16 bytes of code, compared to Object
  58. Pascal which requires 4.  I think this is partly because C++ code is translated
  59. into C, which is then compiled.  It's also a result of trying to make
  60. dispatching as fast as possible.
  61.  
  62. You can reduce the space requirements somewhat by using subclasses of
  63. SingleObject (or HandleObject).  In MPW C++, this provides single inheritance
  64. only, using a simpler runtime implementation.  In this case, each entry in the
  65. vtable is 4 bytes, and a method call is about 10 bytes.
  66.  
  67. I hope this information helps.  As I said, I'm not an expert in the C++
  68. runtime, so you may want to experiment with C++ for yourself.  You can use the
  69. -c option to CPlus to dump out the generated C code, and generate link maps to
  70. see how global data space is used.
  71.  
  72. Larry Rosenstein
  73.  
  74.  
  75.